package com.example.demo;

import lombok.Data;
import lombok.ToString;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/v1/test")
public class test {

    /**
     * ● GET：接口传入id参数，后端返回id*100后的结果
     * ● POST：接口传入{name：”张三”,sex： “男”,age： “18”}，后端返回“张三，男性，18周岁”
     * ● PUT：接口传入{name：”张三”,sex： “男”,age： “18”} ，后端返回{name：”张三”,sex： “男”,age： “18”， mark： “已成年”}
     * ● DELETE： 接口传入{name：”张三”,sex： “男”,age： “18”} ，后端返回，“张三信息已删除“
     */

    @Data
    @ToString
    static class User {
        private String name;
        private String sex;
        private int age;
        private String mark;
    }

    @GetMapping("/get")
//    @RequestMapping(value = "/id",method = RequestMethod.GET)
    public ResponseEntity<?> get(@RequestParam("id") int id){
        System.out.println(id);

        int result = id * 100;
        return ResponseEntity.ok(result);
    }

    @PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> post(@RequestBody User user){
        System.out.println(user);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(user.getName()).append("，");
        stringBuilder.append(user.getSex()).append("，");
        stringBuilder.append(user.getAge());
//        stringBuilder.append(user.getName()).append("，").append(user.getSex()).append("，").append(user.getAge());
        return ResponseEntity.ok(stringBuilder);
    }


    @PutMapping("/put")
    public  ResponseEntity<?> put(@RequestBody User user){
            System.out.println(user);
            user.setMark("已成年");

        return ResponseEntity.ok(user);
}

@DeleteMapping("/del")
    public  ResponseEntity<?> delete(@RequestBody User user,@RequestParam("id")int id){
        System.out.println(user);
        System.out.println(id);
        String result = "张三信息已删除";
        return ResponseEntity.ok(result);
}


}
